#include "TestEngine.h"

CTestEngine::CTestEngine()
    {
    // No implementation required
    }

CTestEngine::~CTestEngine()
    {
    iPeriodic->Cancel();
    delete iPeriodic;
    }

CTestEngine* CTestEngine::NewLC()
    {
    CTestEngine* self = new (ELeave) CTestEngine();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

CTestEngine* CTestEngine::NewL()
    {
    CTestEngine* self = CTestEngine::NewLC();
    CleanupStack::Pop(); // self;
    return self;
    }

void CTestEngine::ConstructL()
    {
    iPeriodic = CPeriodic::NewL(CActive::EPriorityStandard);
    }

HBufC * CTestEngine::ConcatenateL(TDesC const & aFirst, TDesC const & aSecond)
    {
    RBuf buf;
    buf.Create(aFirst.Length() + aSecond.Length());
    buf.Copy(aFirst);
    buf.Append(aSecond);
    HBufC * retBuf = buf.AllocL();
    buf.Close();
    return retBuf;
    }

void CTestEngine::ConcatenateAsyncL(scatu::MFunctor<HBufC *> & aRetFunc, TDesC const & aFirst, TDesC const & aSecond)
    {
    iConcatenateFunctor = &aRetFunc;
    iConcatenateResult = ConcatenateL(aFirst, aSecond);
    iPeriodic->Start(1000 * 1000, 1000 * 1000, TCallBack(&CTestEngine::ConcatenateCallback, this));
    }

TInt CTestEngine::ConcatenateCallback(TAny * aThis)
    {
    CTestEngine * self = static_cast<CTestEngine *>(aThis);
    self->iPeriodic->Cancel();
    (*self->iConcatenateFunctor)(self->iConcatenateResult);
    return EFalse;
    }
